package com.gurid.service.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gurid.common.base.result.ResultCode;
import com.gurid.common.base.util.*;
import com.gurid.service.base.dto.MemberDto;
import com.gurid.service.base.exception.GuridException;
import com.gurid.service.ucenter.entity.Member;
import com.gurid.service.ucenter.entity.vo.LoginVo;
import com.gurid.service.ucenter.entity.vo.RegisterVo;
import com.gurid.service.ucenter.mapper.MemberMapper;
import com.gurid.service.ucenter.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Gurid
 * @since 2024-06-02
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void register(RegisterVo registerVo) {
        //校验参数
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        String mobile = registerVo.getMobile();
        String code = registerVo.getCode();

        if (!StringUtils.hasText(mobile) || !FormUtils.isMobile(mobile)) {
            throw new GuridException(ResultCode.LOGIN_PHONE_ERROR);
        }

        if (!StringUtils.hasText(nickname) || !StringUtils.hasText(password) || !StringUtils.hasText(code)) {
            throw new GuridException(ResultCode.PARAM_ERROR);
        }

        //校验验证码
        String checkCode = (String)redisTemplate.opsForValue().get(mobile);
        /*if (!checkCode.equals(code)) {*/ //把code放前面是因为先看下checCode有没有，否则万一redis里的checkCode过期了就省去比较了
        if (!code.equals(checkCode)) {
            throw new GuridException(ResultCode.CODE_ERROR);
        }

        //查看用户是否已经注册
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile", mobile);
        Long count = baseMapper.selectCount(memberQueryWrapper);
        if (count > 0) {
            throw new GuridException(ResultCode.REGISTER_MOBLE_ERROR);
        }

        //查看昵称是否重复
        /*QueryWrapper<Member> memberQueryWrapper1 = new QueryWrapper<>();
        memberQueryWrapper.eq("nickname", nickname);
        Long count1 = baseMapper.selectCount(memberQueryWrapper1);
        if (count1 > 0) {
            System.out.println("-------" + baseMapper.selectOne(memberQueryWrapper1));
            throw new GuridException(ResultCode.REGISTER_NICKNAME_ERROR);
        }*/

        //开始注册
        Member member = new Member();
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setMobile(mobile);
        member.setAvatar("https://cdn.pixabay.com/photo/2016/04/22/04/57/graduation-1345143_1280.png");//默认头像
        member.setDisabled(false);
        baseMapper.insert(member);


    }

    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        if (!StringUtils.hasText(mobile) || !StringUtils.hasText(password)) {
            throw new GuridException(ResultCode.PARAM_ERROR);
        }

        //校验参数
        if (!FormUtils.isMobile(mobile)) {
            throw new GuridException(ResultCode.LOGIN_PHONE_ERROR);
        }
        //查找手机号
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile", mobile);
        Member member = baseMapper.selectOne(memberQueryWrapper);
        if (member == null) {
            throw new GuridException(ResultCode.LOGIN_PHONE_ERROR);
        }
        //校验密码
        if (!MD5.encrypt(password).equals(member.getPassword())) {
            throw new GuridException(ResultCode.LOGIN_PASSWORD_ERROR);
        }
        //校验用户是否被禁用
        if (member.getDisabled()) {
            throw new GuridException(ResultCode.LOGIN_DISABLED_ERROR);
        }

        //以上校验无误，则登录并生成token
        JwtInfo jwtInfo = new JwtInfo();
        jwtInfo.setId(member.getId())
                .setNickname(member.getNickname())
                .setAvatar(member.getAvatar());
        String token = JwtUtils.getJwtToken(jwtInfo, 1800);
        //redisTemplate.opsForValue().set(mobile, token);
        return token;
    }

    @Override
    public MemberDto getMemberDtoByMemberId(String memberId) {

        Member member = baseMapper.selectById(memberId);
        MemberDto memberDto = new MemberDto();
        BeanUtils.copyProperties(member, memberDto);
        return memberDto;
    }

    @Override
    public Integer countRegisterNum(String day) {
        return baseMapper.selectRegisterCountByDay(day);
    }
}
